﻿// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.IO;
using System.Windows.Forms;
using OpenNETCF.Windows.Forms;
using svs.Mobile.BeyondMedia.FeedCore;
using svs.Mobile.BeyondMedia.Objects;
using svs.Mobile.BeyondMedia.Properties;
using svs.Mobile.BeyondMedia.UiCore;
using svs.Mobile.BeyondScheduler.Core.Objects;

namespace svs.Mobile.BeyondMedia.Forms.Settings
{
	public partial class GeneralSettings : UserControl, ISettingsPage
	{
		public GeneralSettings()
		{
			InitializeComponent();
		}

		public static string PageName
		{
			get { return "General Settings"; }
		}

		public static string PageDescription
		{
			get
			{
				return "Set Download Folder, Sort, Auto Update... ";
			}
		}

		public static SettingsPageHolderForm.SettingsPages PageID
		{
			get { return SettingsPageHolderForm.SettingsPages.General; }
		}

		#region ISettingsPage Members

		public bool CheckAndSaveSettings()
		{
			Configuration.FeedsSortOrder = (CoreHelper.SortOrder) FeedSortOrder.SelectedIndex;
			Configuration.TracksSortOrder = (CoreHelper.SortOrder) TracksSortOrder.SelectedIndex;

			Configuration.UpdateOnStartup = EnableUpdateOnStartup.Checked;
			Configuration.UpdateOnFeedOpen = EnableUpdateOnFeedOpen.Checked;
			Configuration.EnableScreenTransitions = EnableScreenTransitions.Checked;

			string dir = RootDirectory.Text.Trim();
			if (!string.IsNullOrEmpty(dir))
			{
				if (!Directory.Exists(dir))
				{
					if (
						MessageBoxEx.Show(Resources.InvalidFeedsFolderMsg, Resources.InvalidFeedsFolderTitle,
						                  MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button2) !=
						DialogResult.Yes)
						return false;
				}
			}

			Configuration.EnclosureDownloadPath = dir;
			FeedRepository.RootPath = Configuration.EnclosureDownloadPath;

			//enable or disable all Update tasks
			foreach (var task in Helpers.ScheduledUpdateTasks)
			{
				if (EnableAutoUpdate.Checked)
				{
						task.Active = true;

						if (task.Event.ExistOnDevice)
							TaskRepository.DeleteScheduledTaskFromDevice(task);

						TaskRepository.SetScheduledTaskToDevice(task);
				}
				else
				{
					task.Active = false;
					TaskRepository.DeleteScheduledTaskFromDevice(task);
				}
			}

			//If BeyondScheduler has made any changes - do not overwrite them
			if(!TaskRepository.WasRepositoryModifiedExternally())
				TaskRepository.SaveRepository();
			else
				Helpers.Beep();

			Configuration.AllowAutoTrackDeletions =  AllowAutoDeletions.SelectedIndex == 0 ? true: false;
			Configuration.AllowCellularConnections = AllowCellularConnection.Checked;

			return true;
		}

		public void LoadPageSettings()
		{
			RootDirectory.Text = Configuration.EnclosureDownloadPath;
			FeedSortOrder.SelectedIndex = (int) Configuration.FeedsSortOrder;
			TracksSortOrder.SelectedIndex = (int) Configuration.TracksSortOrder;

			EnableUpdateOnStartup.Checked = Configuration.UpdateOnStartup;
			EnableUpdateOnFeedOpen.Checked = Configuration.UpdateOnFeedOpen;

			EnableScreenTransitions.Checked = Configuration.EnableScreenTransitions;

			//Updates are enabled if there is at least one Update task that is active
			EnableAutoUpdate.Checked = false;
			foreach (var task in Helpers.ScheduledUpdateTasks)
			{
				if (task.Active)
				{
					EnableAutoUpdate.Checked = true;
					break;
				}
			}

			UpdateSchedule.Enabled = EnableAutoUpdate.Checked && Helpers.ScheduledUpdateTasks.Count == 1;

			AllowAutoDeletions.SelectedIndex = Configuration.AllowAutoTrackDeletions ? 0 : 1;
			AllowCellularConnection.Checked = Configuration.AllowCellularConnections;
		}

		public bool ShouldReloadRepositoryOnDone
		{
			get { return true; }
		}

		#endregion

		private void SetRootFolder_Click(object sender, EventArgs e)
		{
			FolderBrowserDialog dlg = new FolderBrowserDialog();
			dlg.ShowNewFolderButton = true;
			if (dlg.ShowDialog() == DialogResult.OK)
				RootDirectory.Text = dlg.SelectedPath;
		}

		private void OnEnableAutoUpdatesChanged(object sender, EventArgs e)
		{
		UpdateSchedule.Enabled = EnableAutoUpdate.Checked;
		}

		private void OnUpdateScheduleClicked(object sender, EventArgs e)
		{
				AutoUpdateSettings aus = new AutoUpdateSettings();
				aus.ShowDialog();
		}

		private void AdvancedScheduling_Click(object sender, EventArgs e)
		{
			CommandManager.CmdExecuteCommandLine(Path.Combine(Configuration.ApplicationInstallPath, "BeyondScheduler.exe"));
		}

	}
}